using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace Jonathan.SharpDB
{
	/// <summary>
	/// A program that can view and create SharpDB databases.
	/// </summary>
	public partial class fSharpDBViewer : Form
	{
		/// <summary>
		/// Creates a new SharpDB Viewer.
		/// </summary>
		public fSharpDBViewer ()
		{
			InitializeComponent();
			this.SetupGrid();
		}

		/// <summary>
		/// Setup the way to display the grids.
		/// </summary>
		private void SetupGrid ()
		{
			DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
			cellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.TopLeft;
			cellStyle.BackColor = System.Drawing.SystemColors.Window;
			cellStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			cellStyle.ForeColor = System.Drawing.SystemColors.ControlText;
			cellStyle.SelectionBackColor = System.Drawing.SystemColors.Highlight;
			cellStyle.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
			cellStyle.WrapMode = System.Windows.Forms.DataGridViewTriState.False;

			this._GridData.Tag = "";
			this._GridData.DefaultCellStyle = cellStyle;
			this._GridData.RowHeadersVisible = false;
			this._GridData.BackgroundColor = System.Drawing.Color.White;
			this._GridData.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.SingleHorizontal;
			this._GridData.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically;
			this._GridData.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
			//this._GridData.AutoSize = false;
		}

		/// <summary>
		/// Clears the information in the grid (re-adds the columns)...
		/// </summary>
		public void ClearGrid ()
		{
			this._GridData.BindData(null, null);
			this._GridData.ClearGroups();
			this._GridData.Rows.Clear();
			this._GridData.Columns.Clear();
		}

		/// <summary>
		/// Adds the Columns to the Grids.
		/// </summary>
		private void AddColumns (List<Column> columns)
		{
			foreach (Column col in columns)
			{
				this._GridData.Columns.Add(col.ColumnName, col.ColumnName);
			}
		}

		/// <summary>
		/// Occurs when the File--Exit MenuItem is clicked.
		/// </summary>
		/// <param name="sender">ToolStripMenuItem (_mnuFileExit)</param>
		/// <param name="e">EventArgs</param>
		private void _mnuFileExit_Click (object sender, EventArgs e)
		{
			this.Close();
		}

		/// <summary>
		/// Occurs when the File--Open MenuItem is clicked.
		/// </summary>
		/// <param name="sender">ToolStripMenuItem (_mnuFileOpen)</param>
		/// <param name="e">EventArgs</param>
		private void _mnuFileOpen_Click (object sender, EventArgs e)
		{
			if (this._ofdMain.ShowDialog(this) == DialogResult.OK)
			{
				this.OpenDatabase(this._ofdMain.FileName);
			}
		}

		/// <summary>
		/// Opens the given database for displaying.
		/// </summary>
		/// <param name="dbPath">Path to Database.</param>
		private void OpenDatabase (string dbPath)
		{
			FileInfo f = new FileInfo(dbPath);
			if (f.Extension.Equals(".db"))
			{
				this._mnuTxtDBFile.Text = f.Name;
				this._mnuTxtDBFile.ToolTipText = dbPath;
				Database db = new Database(dbPath);
				string[] tables = db.TableList.Split(';');
				foreach (string table in tables)
				{
					TreeNode rNode = new TreeNode(table);
					List<Column> columns = db.GetTableColumns(table);
					foreach (Column col in columns)
					{
						rNode.Nodes.Add(col.ColumnName + ":" + col.ColumnType);
					}
					this._TreeStructure.Nodes.Add(rNode);
				}

				db.CloseDatabase();
			}
		}

		/// <summary>
		/// Occurs when a node is selected in the Structure TreeView.
		/// </summary>
		/// <param name="sender">TreeNode.</param>
		/// <param name="e">TreeNOdeMouseClickEventArgs</param>
		void _TreeStructure_AfterSelect (object sender, TreeViewEventArgs e)
		{
			if (this._TreeStructure.SelectedNode != null)
			{
				TreeNode tableNode = (this._TreeStructure.SelectedNode.Parent == null) ? this._TreeStructure.SelectedNode : this._TreeStructure.SelectedNode.Parent;
				if (_GridData.Tag.ToString().Equals(tableNode.Text))
					return;

				this.ClearGrid();
				Database db = new Database(this._mnuTxtDBFile.ToolTipText);
				Query q = new Query(QueryType.SELECT, tableNode.Text);
				q.SELECT("*");
				db.RunQuery(q);

				if (q.Results != null)
				{
					this.AddColumns(q.Results.Columns);
					foreach (ArrayList rowInfo in q.Results.Rows)
					{
						OutlookGrid.OutlookGridRow row = new Jonathan.OutlookGrid.OutlookGridRow();
						object[] objs = new object[rowInfo.Count];
						for (int a = 0; a < objs.Length; a++)
							objs[a] = rowInfo[a];
						row.CreateCells(this._GridData, objs);
						this._GridData.Rows.Add(row);
					}
				}
			}
		}

		private void _mnuToolsTextImporter_Click (object sender, EventArgs e)
		{
#if DEBUG
			TextImporter txt = new TextImporter();
			txt.ImportBook();
			MessageBox.Show("DONE");
#endif
		}
	}
}